package com.study.langchain4j.service.rag;

import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author xl-9527
 * @since 2025/11/3
 */
@Service
public class EmbeddingModelService {

    private final EmbeddingModel embeddingModel;
    private final EmbeddingStore<String> embeddingStore;

    public EmbeddingModelService(final EmbeddingModel embeddingModel, final EmbeddingStore<String> embeddingStore) {
        this.embeddingModel = embeddingModel;
        this.embeddingStore = embeddingStore;
    }


    public Embedding toVector(final String text) {
        return embeddingModel.embed(TextSegment.from(text)).content();
    }

    public void writeToVectorDataBase(final String content) {
        if (ObjectUtils.isEmpty(content)) {
            throw new RuntimeException("content is empty");
        }

        // write to vector database
        embeddingStore.add(
                embeddingModel.embed(content).content()
        );
    }
}
